<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Title</title>
  </head>
  <body>
    <script>
      /*
  * 
  *            -20
              / \
             3   2
            / \   \
           8   6   9
           *
           *
           * */
      const tree = {
        val: -20,
        left: {
          val: 3,
          left: { val: 8, left: null, right: null },
          right: { val: 6, left: null, right: null },
        },
        right: {
          val: 2,
          left: null,
           right: { val: 9, left: null, right: null },
        },
      };
      const getPath = (root) => {
        let count = 0;
        const dfs = (root) => {
          if (!root) return 0;
          const left = dfs(root.left);
          const right = dfs(root.right);
          count = Math.max(count, left + right);
          return Math.max(left, right) + 1;
        };
        dfs(root);
        return count;
      };



      var diameterOfBinaryTree = function(root) {
          let res = 0

          function depth (node) {
              if (!node) return 0 // 节点不存在返回0
              let left = depth(node.left) // left为左子树的深度
              let right = depth(node.right)//right 为右子树的深度
              res = Math.max(left + right, res) //计算l+r 更新res
              return Math.max(left, right)+1 //返回该节点为根的子树的深度
          }
          depth(root)
          return res
      };



      console.log(diameterOfBinaryTree(tree));
      console.log(getPath(tree));
    </script>
  </body>
</html>
